package router

import (
	"dhuviewer/dto"
	"dhuviewer/jwt"
	services "dhuviewer/services"
	"fmt"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
	"github.com/google/uuid"
	"github.com/labstack/gommon/log"
)

const per_page int = 6

func Gallery(rg *gin.RouterGroup) {
	galleryRouter := rg.Group("/")
	galleryRouter.GET("/p/:pagenum", func(c *gin.Context) {
		pagenum := c.Param("pagenum")
		fmt.Println(pagenum)
		temp_page, _ := strconv.Atoi(pagenum)
		offset := per_page * (temp_page - 1)
		err, galleryItems := services.GetGallery(int32(offset))
		if err != nil {
			c.JSON(200, gin.H{
				"status": 40000,
			})
			return
		}
		c.JSON(200, gin.H{
			"status": 20000,
			"data":   galleryItems,
		})
	})

	galleryRouter.GET("/view/:gid", func(c *gin.Context) {
		gid := c.Param("gid")
		fmt.Println(gid)
		if gid == "undefined" {
			c.JSON(200, gin.H{
				"status": 40000,
			})
			return
		}
		err, galleryItems := services.GetOneGallery(gid)
		if err != nil {
			c.JSON(200, gin.H{
				"status": 40000,
			})
			return
		}
		c.JSON(200, gin.H{
			"status": 20000,
			"data":   galleryItems,
		})
	})

	galleryRouter.GET("/getMine", func(c *gin.Context) {
		const BEARER_SCHEMA = "Bearer"
		authHeader := c.GetHeader("Authorization")
		tokenString := authHeader[len(BEARER_SCHEMA)+1:]

		res, payload := jwt.CheckJwt(tokenString)
		if !res {
			c.JSON(200, gin.H{
				"status": 40000,
			})
			return
		}
		fmt.Println(payload)
		err, galleryItems := services.GetUserGallery(payload.Uid)
		if err != nil {
			c.JSON(200, gin.H{
				"status": 40000,
			})
			return
		}
		c.JSON(200, gin.H{
			"status": 20000,
			"data":   galleryItems,
		})
	})

	galleryRouter.GET("/getUser/:userid", func(c *gin.Context) {
		userid := c.Param("userid")
		fmt.Println(userid)
		const BEARER_SCHEMA = "Bearer"
		authHeader := c.GetHeader("Authorization")
		tokenString := authHeader[len(BEARER_SCHEMA)+1:]

		res, payload := jwt.CheckJwt(tokenString)
		if !res {
			c.JSON(200, gin.H{
				"status": 40000,
			})
			return
		}
		fmt.Println(payload)
		err, galleryItems := services.GetUserGallery(userid)
		if err != nil {
			c.JSON(200, gin.H{
				"status": 40000,
			})
			return
		}
		c.JSON(200, gin.H{
			"status": 20000,
			"data":   galleryItems,
		})
	})

	galleryRouter.DELETE("/del/:gid", func(c *gin.Context) {
		gid := c.Param("gid")
		fmt.Println(gid)
		const BEARER_SCHEMA = "Bearer"
		authHeader := c.GetHeader("Authorization")
		tokenString := authHeader[len(BEARER_SCHEMA)+1:]
		var err error
		res, payload := jwt.CheckJwt(tokenString)
		if !res {
			c.JSON(200, gin.H{
				"status": 40000,
			})
			return
		}
		if payload.Level == "admin" {
			services.DeleteReplyByGid(uuid.MustParse(gid))
			//外键问题
			err = services.DeleteGallery(uuid.MustParse(gid))
		} else {
			services.DeleteReplyByGid(uuid.MustParse(gid))
			err = services.DeleteOnesGallery(uuid.MustParse(gid), uuid.MustParse(payload.Uid))
		}
		if err != nil {
			log.Info(err)
			c.JSON(200, gin.H{
				"status": 40000,
			})
			return
		}
		c.JSON(200, gin.H{
			"status": 20000,
		})
	})

	galleryRouter.POST("/create", func(c *gin.Context) {
		const BEARER_SCHEMA = "Bearer"
		authHeader := c.GetHeader("Authorization")
		tokenString := authHeader[len(BEARER_SCHEMA)+1:]

		res, payload := jwt.CheckJwt(tokenString)
		if !res || payload.Level == "banned" {
			c.JSON(200, gin.H{
				"status": 40000,
			})
			return
		}
		fmt.Println(payload)
		galleryInfo := new(dto.Gallery) //注意该结构接受的内容
		c.ShouldBind(&galleryInfo)
		err, gid := services.CreateGallery(galleryInfo, payload.Uid, payload.Username)
		if err != nil {
			c.JSON(200, gin.H{
				"status": 40000,
			})
			return
		}
		c.JSON(200, gin.H{
			"status": 20000,
			"gid":    gid.String(),
		})
		return
	})

	galleryRouter.GET("/search/:tags", func(c *gin.Context) {
		tag := c.Param("tags")
		_, err := HelpcheckJWT(c)
		if err != nil {
			return
		}
		items, err := services.SelectByTag(tag)
		HelpErrorWithReturn(c, err, items)
	})

	galleryRouter.GET("/searchMVP/:tags", func(c *gin.Context) {
		tag := c.Param("tags")
		withCount := c.Query("nocount")
		_, err := HelpcheckJWT(c)
		if err != nil {
			return
		}
		if withCount == "" {
			items, err := services.TagMVP(tag)
			HelpErrorWithReturn(c, err, items)
		} else {
			items, err := services.TagMVPWithoutCount(tag)
			HelpErrorWithReturn(c, err, items)
		}
		// fmt.Println(items, err)
	})

	galleryRouter.GET("/stat/galleryByDate", func(c *gin.Context) {
		payload, err := HelpcheckJWT(c)
		if err != nil {
			return
		}
		res, err := services.StatGallery(uuid.MustParse(payload.Uid))
		HelpErrorWithReturn(c, err, res)
	})

	galleryRouter.POST("/fliter/time", func(c *gin.Context) {
		_, err := HelpcheckJWT(c)
		if err != nil {
			return
		}
		json := make(map[string]interface{}) //注意该结构接受的内容
		c.BindJSON(&json)
		// fmt.Println(json)
		const TIME_LAYOUT = "2006-01-02T15:04:05Z"
		begin := json["begin"].(string)
		end := json["end"].(string)
		beginTime, err := time.Parse(TIME_LAYOUT, begin)
		endTime, err := time.Parse(TIME_LAYOUT, end)
		if err != nil {
			fmt.Println(err)
			c.JSON(200, gin.H{
				"status": 40000,
				"info":   "time-Format Error",
			})
			return
		}
		// fmt.Println(beginTime, endTime)
		items, err := services.SelectByTimeRange(beginTime, endTime)
		HelpErrorWithReturn(c, err, items)
	})
}
